package com.vegetable.modules.service.bank.convert;

import com.alibaba.fastjson.JSONObject;
import com.vegetable.modules.entity.bank.vo.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 交行流水标准化字段输出
 */
public class BCMConverter {
    private static final String bankMark = "BCM";
    public String convert1(String json) throws Exception {
        BCM bcm = JSONObject.parseObject(json, BCM.class);
        String standardizedJson = "";

        StandardizedBank standardizedBank = new StandardizedBank();
        standardizedBank.setAccountName(bcm.getAccountName());
        standardizedBank.setCardNumber(bcm.getAccountNo());
        standardizedBank.setIdNumber(bcm.getIdNo());
        standardizedBank.setStartDate(bcm.getQueryStartingDate());
        standardizedBank.setEndDate(bcm.getQueryEndingDate());
        standardizedBank.setPrintTime(bcm.getPrintingDate() + " " + bcm.getPrintingTime());

        List<StandardizedBankTran> standardizedBankTrans = new ArrayList<>();
        for (BCMTran bcmTran : bcm.getBcmTrans()) {
            StandardizedBankTran standardizedBankTran = new StandardizedBankTran();
            standardizedBankTran.setTranDate(bcmTran.getTransDate());
            standardizedBankTran.setTranTime(bcmTran.getTransTime());
            standardizedBankTran.setCurrency(bcm.getCurrency());
            standardizedBankTran.setAmount(bcmTran.getDcFlg().contains("贷") ? bcmTran.getTransAmt() : "-" + bcmTran.getTransAmt());
            standardizedBankTran.setBalance(bcmTran.getBalance());
            standardizedBankTran.setTranName(null);
            standardizedBankTran.setChannel(null);
            standardizedBankTran.setPostscript(bcmTran.getSummary());
            standardizedBankTran.setCounterpartInfo(bcmTran.getPaymentReceiptAccountName() + "/" + bcmTran.getPaymentReceiptAccount());
            standardizedBankTrans.add(standardizedBankTran);
        }
        standardizedBank.setStandardizedBankTrans(standardizedBankTrans);

        standardizedJson = JSONObject.toJSONString(standardizedBank);
        return standardizedJson;
    }

    public String convert2(String json) throws Exception {
        BCM2 bcm2 = JSONObject.parseObject(json, BCM2.class);
        String standardizedJson = "";

        StandardizedBank standardizedBank = new StandardizedBank();
        standardizedBank.setAccountName(bcm2.getAccountName());
        standardizedBank.setCardNumber(bcm2.getAccountNo());
        standardizedBank.setIdNumber(bcm2.getIdNo());
        standardizedBank.setStartDate(bcm2.getQueryStartingDate());
        standardizedBank.setEndDate(bcm2.getQueryEndingDate());
        standardizedBank.setPrintTime(bcm2.getPrintingTime());

        List<StandardizedBankTran> standardizedBankTrans = new ArrayList<>();
        for (BCMTran2 bcmTran2 : bcm2.getBcmTrans()) {
            StandardizedBankTran standardizedBankTran = new StandardizedBankTran();
            standardizedBankTran.setTranDate(bcmTran2.getTransDate());
            standardizedBankTran.setTranTime(null);  // 源对象中没有交易时间字段
            standardizedBankTran.setCurrency(bcm2.getCurrency());  // 源对象中没有币种字段
            standardizedBankTran.setAmount(bcmTran2.getDcFlg().contains("贷") ? bcmTran2.getTransAmt() : "-" + bcmTran2.getTransAmt());
            standardizedBankTran.setBalance(bcmTran2.getBalance());
            standardizedBankTran.setTranName(null);
            standardizedBankTran.setChannel(null);
            standardizedBankTran.setPostscript(null);
            standardizedBankTran.setCounterpartInfo(null);  // 源对象中没有对手信息字段
            standardizedBankTrans.add(standardizedBankTran);
        }
        standardizedBank.setStandardizedBankTrans(standardizedBankTrans);

        standardizedJson = JSONObject.toJSONString(standardizedBank);
        return standardizedJson;
    }

    public String convert(String json) throws Exception {
        if (json.contains("对方户名")) {
            return convert1(json);
        } else {
            return convert2(json);
        }
    }

    public static void main(String[] args) throws Exception {
        BCMConverter bcmConverter = new BCMConverter();
        String bcmJson1 = "{\"部门\":\"01443036999\",\"柜员\":\"EFC0003\",\"打印日期\":\"2022-11-03 17:28:38\",\"打印时间\":\"2022-11-03 17:28:38\",\"账号/卡号\":\"6222621310042733523\",\"户名\":\"陈明艳\",\"查询起日\":\"2021-11-03\",\"查询止日\":\"2022-11-03\",\"查询时间\":\"2022年11月03日  17:29:14\",\"查询柜员\":\"EFC0003\",\"证件种类\":\"第二代居民身份证\",\"证件号码\":\"350721199501281826\",\"币种\":\"人民币 CNY\",\"交易明细\":[{\"序号\":\"1\",\"交易日期\":\"2021-12-21\",\"交易时间\":\"00:00:00\",\"交易类型\":\"存款利息\",\"借贷\":\"贷 Cr\",\"交易金额\":\"1.45\",\"余额\":\"2.16\",\"对方账户\":\"443036012710300100001599\",\"对方户名\":\"应付个人活期储蓄存款利息\",\"交易地点\":\"批处理\",\"摘要\":\"\"},{\"序号\":\"2\",\"交易日期\":\"2021-12-21\",\"交易时间\":\"05:46:01\",\"交易类型\":\"信用卡转账还款\",\"借贷\":\"借 Dr\",\"交易金额\":\"2.16\",\"余额\":\"0.00\",\"对方账户\":\"6222526311864659\",\"对方户名\":\"陈明艳\",\"交易地点\":\"批处理\",\"摘要\":\"\"},{\"序号\":\"3\",\"交易日期\":\"2022-07-14\",\"交易时间\":\"11:35:30\",\"交易类型\":\"代发其他款项\",\"借贷\":\"贷 Cr\",\"交易金额\":\"20,000.00\",\"余额\":\"20,000.00\",\"对方账户\":\"443899991010004402842\",\"对方户名\":\"深圳前海微众银行股份有限公司\",\"交易地点\":\"第三方系统\",\"摘要\":\"\"},{\"序号\":\"4\",\"交易日期\":\"2022-07-14\",\"交易时间\":\"15:22:55\",\"交易类型\":\"代发其他款项\",\"借贷\":\"贷 Cr\",\"交易金额\":\"10,000.00\",\"余额\":\"30,000.00\",\"对方账户\":\"443899991010004402842\",\"对方户名\":\"深圳前海微众银行股份有限公司\",\"交易地点\":\"第三方系统\",\"摘要\":\"\"},{\"序号\":\"5\",\"交易日期\":\"2022-07-15\",\"交易时间\":\"10:19:52\",\"交易类型\":\"跨行汇款\",\"借贷\":\"贷 Cr\",\"交易金额\":\"4.00\",\"余额\":\"30,004.00\",\"对方账户\":\"6236330014122171412\",\"对方户名\":\"陈明艳\",\"交易地点\":\"网上银行\",\"摘要\":\"\"},{\"序号\":\"6\",\"交易日期\":\"2022-07-15\",\"交易时间\":\"10:20:23\",\"交易类型\":\"代扣其他款项\",\"借贷\":\"借 Dr\",\"交易金额\":\"30,003.50\",\"余额\":\"0.50\",\"对方账户\":\"443899991010004402842\",\"对方户名\":\"深圳前海微众银行股份有限公司\",\"交易地点\":\"第三方系统\",\"摘要\":\"\"},{\"序号\":\"7\",\"交易日期\":\"2022-07-15\",\"交易时间\":\"15:50:28\",\"交易类型\":\"代发其他款项\",\"借贷\":\"贷 Cr\",\"交易金额\":\"500.00\",\"余额\":\"500.50\",\"对方账户\":\"443899991010004402842\",\"对方户名\":\"深圳前海微众银行股份有限公司\",\"交易地点\":\"第三方系统\",\"摘要\":\"\"},{\"序号\":\"8\",\"交易日期\":\"2022-07-16\",\"交易时间\":\"11:26:27\",\"交易类型\":\"其他交易\",\"借贷\":\"借 Dr\",\"交易金额\":\"500.18\",\"余额\":\"0.32\",\"对方账户\":\"340440360120002\",\"对方户名\":\"(特约)微粒贷(信贷还款)\",\"交易地点\":\"(特约)微粒贷(信贷还款)\",\"摘要\":\"\"},{\"序号\":\"9\",\"交易日期\":\"2022-09-21\",\"交易时间\":\"00:00:00\",\"交易类型\":\"存款利息\",\"借贷\":\"贷 Cr\",\"交易金额\":\"0.21\",\"余额\":\"0.53\",\"对方账户\":\"443036012710300100001599\",\"对方户名\":\"应付个人活期储蓄存款利息\",\"交易地点\":\"批处理\",\"摘要\":\"\"}]}";

        String bcmJson2 = "{\"账号/卡号\":\"6222621310042733523\",\"户名\":\"陈明艳\",\"查询起日\":\"2021-06-20\",\"查询止日\":\"2022-06-20\",\"打印时间\":\"2022-06-20 16:42:59\",\"柜员号\":\"EFC0002\",\"证件种类\":null,\"证件号码\":null,\"币种\":\"人民币 CNY\",\"交易明细\":[{\"交易日期\":\"2021-07-01\",\"交易地点\":\"财付通支付科技有限公司-\",\"交易方式\":\"其他交易\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"10.00\",\"余额\":\"10.00\"},{\"交易日期\":\"2021-07-02\",\"交易地点\":\"批处理\",\"交易方式\":\"借记卡工本费\",\"借贷状态\":\"借Dr\",\"交易金额\":\"5.00\",\"余额\":\"5.00\"},{\"交易日期\":\"2021-07-25\",\"交易地点\":\"财付通支付科技有限公司\",\"交易方式\":\"网上支付\",\"借贷状态\":\"借Dr\",\"交易金额\":\"5.00\",\"余额\":\"0.00\"},{\"交易日期\":\"2021-09-07\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.66\",\"余额\":\"0.66\"},{\"交易日期\":\"2021-09-07\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.16\",\"余额\":\"0.82\"},{\"交易日期\":\"2021-09-07\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.13\",\"余额\":\"0.95\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.10\",\"余额\":\"0.85\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.01\",\"余额\":\"0.84\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.02\",\"余额\":\"0.82\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.03\",\"余额\":\"0.79\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.02\",\"余额\":\"0.77\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.06\",\"余额\":\"0.71\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.01\",\"余额\":\"0.70\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.02\",\"余额\":\"0.68\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"0.04\",\"余额\":\"0.64\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.00\",\"余额\":\"1.64\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.55\",\"余额\":\"2.19\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.40\",\"余额\":\"2.59\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.60\",\"余额\":\"3.19\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.20\",\"余额\":\"4.39\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.60\",\"余额\":\"5.99\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.80\",\"余额\":\"6.79\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.80\",\"余额\":\"8.59\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.90\",\"余额\":\"10.49\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.90\",\"余额\":\"11.39\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.09\",\"余额\":\"11.48\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.90\",\"余额\":\"12.38\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.60\",\"余额\":\"12.98\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.70\",\"余额\":\"13.68\"},{\"交易日期\":\"2021-09-16\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.11\",\"余额\":\"13.79\"},{\"交易日期\":\"2021-09-20\",\"交易地点\":\"网上银行\",\"交易方式\":\"跨行汇款\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"5,100.00\",\"余额\":\"5,113.79\"},{\"交易日期\":\"2021-09-21\",\"交易地点\":\"批处理\",\"交易方式\":\"存款利息\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"0.04\",\"余额\":\"5,113.83\"},{\"交易日期\":\"2021-10-21\",\"交易地点\":\"批处理\",\"交易方式\":\"信用卡转账还款\",\"借贷状态\":\"借Dr\",\"交易金额\":\"1,067.12\",\"余额\":\"4,046.71\"},{\"交易日期\":\"2021-10-26\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"46.00\",\"余额\":\"4,000.71\"},{\"交易日期\":\"2021-10-26\",\"交易地点\":\"手机银行\",\"交易方式\":\"汇兑\",\"借贷状态\":\"借Dr\",\"交易金额\":\"4,000.00\",\"余额\":\"0.71\"},{\"交易日期\":\"2021-12-21\",\"交易地点\":\"批处理\",\"交易方式\":\"存款利息\",\"借贷状态\":\"贷Cr\",\"交易金额\":\"1.45\",\"余额\":\"2.16\"},{\"交易日期\":\"2021-12-21\",\"交易地点\":\"批处理\",\"交易方式\":\"信用卡转账还款\",\"借贷状态\":\"借Dr\",\"交易金额\":\"2.16\",\"余额\":\"0.00\"}]}";

        String standardizedJson = bcmConverter.convert(bcmJson1);
        System.out.println(standardizedJson);
    }

}
